import threading
import time
from tkinter import *

import bin_storage
import const
import file_mgmt


# import win32gui


class Window:

    def __init__(self, b2, fontsize, MF_name):
        self.window = Toplevel()
        self.topmost = const.SIG_topmost_1
        self.window.wm_attributes('-topmost', self.topmost)
        title = 'f10a'
        self.window.title(title)

        self.MF_channel = MF_name
        self.b2 = b2
        self.labelfont = ('monospaced', fontsize, '')
        ##
        self.name = Label(self.window, text='', borderwidth=0, relief="groove", anchor='w')
        self.name.config(font=('raster', fontsize - 2))
        self.name.grid(row=0, column=0, padx=0, pady=0, columnspan=3)
        ##
        self.a = Label(self.window, text='', borderwidth=2, relief="groove", anchor='w')
        self.a.config(font=self.labelfont, wraplength=500)
        self.a.grid(row=1, column=0, padx=2, pady=2)
        ##
        self.b = Label(self.window, text='', borderwidth=2, relief="groove", anchor='w', fg='blue')
        self.b.config(font=self.labelfont, wraplength=500)
        self.b.grid(row=1, column=1, padx=2, pady=2)
        ##
        self.c = Label(self.window, text='', borderwidth=2, relief="groove", anchor='w')
        self.c.config(font=self.labelfont, wraplength=500)
        self.c.grid(row=1, column=2, padx=2, pady=2)

        ##
        self.labels = {'name': self.name, 'col1': self.a, 'col2': self.b, 'col3': self.c
                       }
        ##
        self.stock_name = ''
        self.stock_hwnd = 0
        self.window.protocol("WM_DELETE_WINDOW", self.on_closing)

        self.dic = bin_storage.Bin_storage().read(file_mgmt.push(dir='f10a', ext="b")[1])

    def on_closing(self):
        self.b2.config(state=NORMAL)

        self.window.destroy()

    def update_text(self, triplet_list, name):
        self.labels['name']['text'] = name
        self.labels['col1']['text'] = triplet_list[0]
        self.labels['col2']['text'] = triplet_list[1]
        self.labels['col3']['text'] = triplet_list[2]
        self.window.update()

    def run_fn(self):
        selected_names = list(self.dic.keys())

        while True:
            name, sig = self.MF_channel

            if (name in selected_names) and (name != self.stock_name):
                self.stock_name = name
                self.update_text(self.dic[self.stock_name], self.stock_name)
            elif (name not in selected_names) and (name != self.stock_name):
                self.update_text(['', '', ''], name + ' not selected')

            if sig == const.SIG_EXIT:
                self.window.destroy()
            elif sig == const.SIG_topmost_1 or sig == const.SIG_topmost_0:
                if sig != self.topmost:
                    self.window.wm_attributes('-topmost', sig)
                    self.topmost = sig
                    if sig == const.SIG_topmost_0:
                        self.window.lower()
            time.sleep(0.3)

    def run(self):
        t = threading.Thread(target=self.run_fn)
        t.start()


if __name__ == '__main__':
    w = Window(None, 16)
    t = threading.Thread(target=w.run_fn)
    t.start()
